package com.dps.omc.publish;

import com.dps.omc.enumer.EnumJobState;
import com.dps.omc.enumer.EnumTeam;
import com.dps.omc.model.*;
import com.dps.omc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by shurrik on 2016/1/7.
 */
@SuppressWarnings("unchecked")
@Service("JobManager")
public class JobManager {
    @Autowired
    private IJobService jobService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private TaskManager taskManager;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentNodeService agentNodeService;
    @Autowired
    private IJobBizTypeService jobBizTypeService;
    @Autowired
    private IJobBizUnitService jobBizUnitService;
    @Autowired
    private IJobArtifactService jobArtifactService;
    @Autowired
    private IJobPreScriptService jobPreScriptService;
    @Autowired
    private ITemplateShellService templateShellService;

    public void createNew(Job job, List<String> bizTypes, List<String> bizUnits, List<JobArtifact> jobArtifacts, List<String> preScriptIds, Map<String,Date> planDateMap, User lastModified)
    {
        //创建作业,
        jobService.add(job,lastModified);
        //创建作业业务类型
        jobBizTypeService.create(job,bizTypes);
        //创建作业业务单元
        jobBizUnitService.create(job,bizUnits);
        //创建作业构件
        jobArtifactService.create(job,jobArtifacts);
        //创建u前置脚本
        jobPreScriptService.create(job,preScriptIds);
        List<Agent> agents = agentService.findListByBizUnits(bizUnits);

        int seq = 1;
        for(JobArtifact jobArtifact:jobArtifacts)
        {
                for(Agent agent:agents)
                {
                    List<String> artifactBizTypes = new ArrayList<>();
                    artifactBizTypes.add(jobArtifact.getBizType());
                    List<AgentNode> agentNodes = agentNodeService.findListByAgent(agent.getId(),artifactBizTypes,jobArtifact.getGroupType());
                    for(AgentNode agentNode:agentNodes) {

                        if(job.getIscron())
                        {
                            Date planDate = planDateMap.get(agent.getBizUnitName());
                            agentNode.setPlanDate(planDate);
                        }
                        taskManager.create(job,agent,agentNode,jobArtifact,seq,lastModified);
                        seq++;
                    }

                }
        }

    }

    public void run(Job job,User lastModified) throws Exception {

        if(job.getIscron())
        {
//            this.runSchedule(job);
            //修改作业状态
            job.setStatus(EnumJobState.READY);
            jobService.update(job,lastModified);
        }
        else
        {
            this.runImmediate(job,lastModified);
        }
    }

    public void runByTeam(Job job,EnumTeam team,EnumJobState jobState,User lastModified) throws Exception {

        List<Task> tasks  = taskService.findNewByJobAndTeam(job,team);
        for(Task task:tasks)
        {
            taskManager.run(job,task,lastModified);
        }
        //修改作业状态
        job.setStatus(jobState);
        jobService.update(job,lastModified);
    }

    //立即执行
    public void runImmediate(Job job,User lastModified) throws Exception
    {
//        List<Task> tasks  = taskService.findList("jobId",job.getId());
        List<Task> tasks  = taskService.findNewByJob(job);
        for(Task task:tasks)
        {
            taskManager.run(job,task,lastModified);
        }
        //修改作业状态
        job.setStatus(EnumJobState.EXECUTED);
        jobService.update(job,lastModified);
    }

    //计划执行
    public void runSchedule(Job job,User lastModified) throws Exception
    {
        if(job.getIscron())
        {
            List<Task> tasks  = taskService.findUnfinishAndExpiredByJob(job);
            for(Task task:tasks)
            {
                taskManager.run(job,task,lastModified);
            }
            Boolean  allFinish = taskService.allFinish(job);
            if(allFinish) //如果全部任务结束
            {
                //修改作业状态
                job.setStatus(EnumJobState.EXECUTED);
                jobService.update(job,lastModified);
            }
        }
    }

}
